﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI.WebControls;

namespace Portal.CMS
{
    public class UploadLawDocumentItem : WebPart
    {
      
        public override string WebPartId
        {
            get
            {
                return "UploadLawDocumentItem";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Upload tài liệu gắn kèm");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        System.Web.UI.WebControls.Button SaveButtonLargeOnly = null;
        System.Web.UI.WebControls.Label labelError = null;


        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl, System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UploadLawDocumentItem));

            labelError = new System.Web.UI.WebControls.Label();
            labelError.CssClass = "ErrorUploadNewsImage";
        
            SaveButtonLargeOnly = new Button();
            SaveButtonLargeOnly.Text = ResourceBLL.GetText(WebPartId, "Upload", "Upload tài liệu");
            SaveButtonLargeOnly.Click += new EventHandler(SaveButtonLargeOnly_Click);



            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetHeaderFormDialog(Title)));
            pl.Controls.Add(labelError);
            pl.Controls.Add(new System.Web.UI.LiteralControl("<table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText(WebPartId,"FileName","Upload file:")));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<input name=\"txtItemName\" id=\"txtItemName\" type=\"file\" runat=server>"));
            pl.Controls.Add(SaveButtonLargeOnly);
            pl.Controls.Add(new System.Web.UI.LiteralControl("<input type=button onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "CloseButton", "Thoát") + "\">"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));

            
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td></td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetFooterFormDialog()));
        }

        void SaveButtonLargeOnly_Click(object sender, EventArgs e)
        {
            string FileSave = "";
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                string LawDocumentItemId = (string)System.Web.HttpContext.Current.Request["LawDocumentItemId"];
                LawDocumentItemCls LawDocumentItemCls = new LawDocumentBLL().CreateModelItem(LawDocumentItemId);

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateItem", LawDocumentItemId, LawDocumentItemCls.Owner, true);

                labelError.Text = "";
                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

                HttpPostedFile hf = (HttpPostedFile)files[0];

                if (hf.FileName.Trim().Length > 0)
                {
                    string PathSave = Environments.WorkingPath + "\\media\\" + Environments.PortalAlias;
                    System.IO.Directory.CreateDirectory(PathSave);

                    PathSave = Environments.WorkingPath + "\\media\\" + Environments.PortalAlias + "\\LawDocument";
                    System.IO.Directory.CreateDirectory(PathSave);

                    FileSave = PathSave + "\\" + hf.FileName;
                    //string UploadName = "";
                    hf.SaveAs(FileSave);

                    ServerSideUpload(LawDocumentItemId, FileSave);
                    try
                    {
                        if (FileSave.Trim().Length > 0)
                        {
                            System.IO.File.Delete(FileSave);
                        }
                    }
                    catch { }
                    try
                    {
                        System.IO.Directory.Delete(PathSave);
                    }
                    catch { }
                }

                
                labelError.Text = ResourceBLL.GetText(WebPartId, "UploadOk", "Thêm mới tài liệu vào thành công!");
            }
            catch (Exception ex)
            {
                try
                {
                    if (FileSave.Trim().Length > 0)
                    {
                        System.IO.File.Delete(FileSave);
                    }
                }
                catch { }
                labelError.Text = ex.Message.ToString();
            }
        }

      

        [AjaxPro.AjaxMethod]
        public string ServerSideUpload(string LawDocumentItemId, string UploadFile)
        {
            byte[] b = Environments.GetBytesFromImageFile(UploadFile);
            new LawDocumentBLL().UpdateAttachFile(null, LawDocumentItemId, b, new System.IO.FileInfo(UploadFile).Name);
            return ResourceBLL.GetText(WebPartId, "MessageUploadOk", "Upload thành công!");
        }

    }
}